package sad.visao;

import java.awt.Color;
import java.awt.event.WindowListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import sad.util.DAOEvent;


public class CadDisciplinaDialog extends DialogAbstrato {

    public CadDisciplinaDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveDisciplina = new javax.swing.JPanel();
        txtNome = new javax.swing.JTextField();
        txtAbreviatura = new javax.swing.JTextField();
        listaProfessores = new javax.swing.JComboBox();
        lblNome = new javax.swing.JLabel();
        lblAbreviatura = new javax.swing.JLabel();
        lblProfessor = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        btnCadastrar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SAD - Cadastro");

        saveDisciplina.setBorder(javax.swing.BorderFactory.createTitledBorder("Salvar Disciplina"));

        listaProfessores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Lista vazia>" }));

        lblNome.setText("Nome:");

        lblAbreviatura.setText("Abreviatura:");

        lblProfessor.setText("Professor:");

        javax.swing.GroupLayout saveDisciplinaLayout = new javax.swing.GroupLayout(saveDisciplina);
        saveDisciplina.setLayout(saveDisciplinaLayout);
        saveDisciplinaLayout.setHorizontalGroup(
            saveDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveDisciplinaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(saveDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblNome)
                    .addComponent(lblProfessor)
                    .addGroup(saveDisciplinaLayout.createSequentialGroup()
                        .addComponent(lblAbreviatura)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 172, Short.MAX_VALUE))
                    .addGroup(saveDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNome, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtAbreviatura, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(listaProfessores, javax.swing.GroupLayout.Alignment.LEADING, 0, 224, Short.MAX_VALUE))
                    .addComponent(lblMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE))
                .addContainerGap())
        );
        saveDisciplinaLayout.setVerticalGroup(
            saveDisciplinaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveDisciplinaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAbreviatura)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtAbreviatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblProfessor)
                .addGap(12, 12, 12)
                .addComponent(listaProfessores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE))
        );

        btnCadastrar.setText("Cadastrar");
        btnCadastrar.setActionCommand("Salvar Disciplina");

        btnExcluir.setText("Excluir");
        btnExcluir.setVisible(false);

        btnCancelar.setText("Cancelar");
        btnCancelar.setActionCommand("Cancelar Disciplina");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(saveDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                    .addComponent(btnExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(saveDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JLabel lblAbreviatura;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblProfessor;
    private javax.swing.JComboBox listaProfessores;
    private javax.swing.JPanel saveDisciplina;
    private javax.swing.JTextField txtAbreviatura;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

   // <editor-fold defaultstate="collapsed" desc="Responde a eventos DAO">
    @Override
    public void modelDAOChange(DAOEvent evt) {
        switch(evt.getID()){
           case DAOEvent.DAO_CREATE:
                resetDialog();
                lblMessage.setForeground(Color.GREEN);
                lblMessage.setText("Disciplina"
                    + " cadastrada com sucesso!!!");
                break;
               
           case DAOEvent.DAO_EXISTE:
               JOptionPane.showMessageDialog(this, "A disciplina "
                    + evt.getSource().toString() + " já existe!!!", null, 
                                            JOptionPane.WARNING_MESSAGE);
                resetDialog();
                break;
               
           case DAOEvent.DAO_ERROFIELD:
               JOptionPane.showMessageDialog(this, "O campo " + evt.getField()
                       + " não pode ficar em branco!!!", null,
                                    JOptionPane.WARNING_MESSAGE);
               focusField(evt.getField());
               break;
        }
    }// </editor-fold>
   
   // <editor-fold defaultstate="collapsed" desc="Retorna dados do objeto">
    
    public String getAbreviatura() {
        return this.txtAbreviatura.getText();
    }
    
    public String getNome() {
        return this.txtNome.getText();
    }
    
    public String getProfessor(){
        String nome_login = (String) listaProfessores.getSelectedItem();
        int barra = nome_login.lastIndexOf("/");
        String login = nome_login.substring(barra+1);
        return login;
    }// </editor-fold>
    
   //<editor-fold defaultstate="collapsed" desc="Métodos públicos comuns a todos os Dialogs com listas">
    @Override
    public void addOuvintes() {
        btnCadastrar.addActionListener(getSuporteEventos());
        btnExcluir.addActionListener(getSuporteEventos());
        btnCancelar.addActionListener(getSuporteEventos());
        this.addWindowOuvinte(getSuporteEventos());
    }
    
    @Override
    public void close() {
        this.resetDialog();
        this.dispose();
    }
    
    @Override
    public void setEditableDados(Object[] d) {
        if(d!=null){
            txtNome.setText(d[0].toString());
            txtAbreviatura.setText(d[1].toString());
            txtAbreviatura.setEnabled(false);
            listaProfessores.setSelectedItem(d[2].toString());
            btnExcluir.setVisible(true);
            btnCadastrar.setText("Alterar");
            btnCadastrar.setActionCommand("Alterar");
            saveDisciplina.setBorder(
                    javax.swing.BorderFactory.createTitledBorder("Alterar Professor"));
            btnCancelar.setText("Voltar");
        }
    }
    
   public void reload(Vector<String> s){
        listaProfessores.setModel(new DefaultComboBoxModel(s));
    }//</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Métodos privados, mas comuns a todos os Dialogs">
    @Override
    protected void resetDialog() {
        txtNome.setText(null);
        txtAbreviatura.setText(null);
        lblMessage.setText(null);
        txtNome.requestFocusInWindow();
    }
    
    @Override
    protected void focusField(String field) {
        if(field.equals("Nome")){
            txtNome.requestFocusInWindow();
        }
        if(field.equals("Abreviatura")){
            txtAbreviatura.requestFocusInWindow();
        }
        if(field.equals("Professor")){
            listaProfessores.requestFocusInWindow();
        }
    }//</editor-fold>
}
